﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW.BLL
{
    public class SendEmailService
    {
        public void SendEmailForOverdueQuatationApplication()
        {
            ApplicationService appService = new ApplicationService();
            List<ApplicationForm> forms = appService.GetApplicationQuatationOverdue();
            UserService userService = new UserService();
            List<User> users = userService.GetAdminAndQuatationer();
            if (forms.Count > 0)
            {
                string emailContent = GetContent(forms);
                foreach (User user in users)
                {
                    // 发送报价预警邮件
                    Console.WriteLine(string.Format("发送报价预警邮件:{0},{1}", user.UserEmail, emailContent));
                    EmailService.SendEmail(user.UserEmail, "报价预警", emailContent);
                }               
            }
        }

        public void SendEmailForRepairOverdue()
        {
            ApplicationProductService productservice = new ApplicationProductService();
            List<RepairOverdueModel> overductModels = productservice.GetRepairOverdueProduct();

            foreach (RepairOverdueModel model in overductModels)
            {
                string body = "{0}工程师，您好，您名下处理的RMA/DOA # {1}已经超过{2}天，请马上处理，以免影响维修周期和客户满意度，谢谢配合，祝工作愉快！！！";
                EmailService.SendEmail(model.EngineerEmail, "预警通知", string.Format(body, model.EngineerName, model.RMACode, model.OverdueDay));
            }
        }

        public void SendEmailForFCRepairOverdue()
        {
            ReturnToFactoryService returnService = new ReturnToFactoryService();
            List<RepairOverdueModel> overductModels = returnService.GetFCOverdueProduct();
            foreach (RepairOverdueModel model in overductModels)
            {
                string body = "{0}工程师，您好，神州数码客服系统提醒您，您受理的工厂RMA# {1}已经到达{2}天，请马上处理，以免影响维修周期和客户满意度，谢谢配合，祝工作愉快！！！";
                EmailService.SendEmail(model.EngineerEmail, "预警通知", string.Format(body, model.EngineerName, model.RMACode, model.OverdueDay));
            }
        }

        private string GetContent(List<ApplicationForm> forms)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table><tr><td>RMA#</td><td>申请人</td><td>电话</td></tr>");
            foreach (ApplicationForm form in forms)
            {
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
                sb.Append("<tr><td>" + form.ApplicationCode + "</td><td>" + form.User.FullName + "</td><td>" + form.User.UserPhone + "</td></tr>");
            }
            sb.Append("</table>");
            return sb.ToString();
        }
    }
}
